<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('autohomeVehicleModel:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="系统id">
                <a-input v-model="queryParam.systemId" allow-clear placeholder="请输入系统id"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="汽车之家车型id">
                <a-input v-model="queryParam.qczjModelId" allow-clear placeholder="请输入汽车之家车型id"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="品牌id">
                  <a-input v-model="queryParam.brandId" allow-clear placeholder="请输入品牌id"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="品牌名称">
                  <a-input v-model="queryParam.brandName" allow-clear placeholder="请输入品牌名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车系id">
                  <a-input v-model="queryParam.seriesId" allow-clear placeholder="请输入车系id"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车系名称">
                  <a-input v-model="queryParam.seriesName" allow-clear placeholder="请输入车系名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车型名称">
                  <a-input v-model="queryParam.modelName" allow-clear placeholder="请输入车型名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="由于车辆型号是从车行168（大搜车旗下）爬取，所以用此ID与其对应。">
                  <a-input v-model="queryParam.dscModelId" allow-clear placeholder="请输入由于车辆型号是从车行168（大搜车旗下）爬取，所以用此ID与其对应。"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车型配置信息">
                  <a-input v-model="queryParam.modelJson" allow-clear placeholder="请输入车型配置信息"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车型编码">
                  <a-input v-model="queryParam.modelCode" allow-clear placeholder="请输入车型编码"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="外观颜色">
                  <a-input v-model="queryParam.appearanceColor" allow-clear placeholder="请输入外观颜色"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="内饰颜色">
                  <a-input v-model="queryParam.interiorColor" allow-clear placeholder="请输入内饰颜色"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="销售类型">
                  <a-input v-model="queryParam.saleType" allow-clear placeholder="请输入销售类型"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="年款">
                  <a-input v-model="queryParam.modelYear" allow-clear placeholder="请输入年款"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="排量(车型首页)">
                  <a-input v-model="queryParam.exhaust" allow-clear placeholder="请输入排量(车型首页)"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="变速箱（车型首页）">
                  <a-input v-model="queryParam.shiftBox" allow-clear placeholder="请输入变速箱（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="上市日期">
                  <a-input v-model="queryParam.launchDate" allow-clear placeholder="请输入上市日期"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="车身结构">
                  <a-input v-model="queryParam.bodyTypeName" allow-clear placeholder="请输入车身结构"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="发动机类型">
                  <a-input v-model="queryParam.engineTypeName" allow-clear placeholder="请输入发动机类型"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="级别（车型首页）">
                  <a-input v-model="queryParam.carGrade" allow-clear placeholder="请输入级别（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="汽车类型">
                  <a-input v-model="queryParam.carType" allow-clear placeholder="请输入汽车类型"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="子品牌id">
                  <a-input v-model="queryParam.subBrandNo" allow-clear placeholder="请输入子品牌id"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="子品牌名称">
                  <a-input v-model="queryParam.subBrandName" allow-clear placeholder="请输入子品牌名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="环保标准（车型首页）">
                  <a-input v-model="queryParam.emissionStandard" allow-clear placeholder="请输入环保标准（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="综合油耗(工信部)（车型首页）">
                  <a-input v-model="queryParam.oilConsumption" allow-clear placeholder="请输入综合油耗(工信部)（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="最大功率（车型首页）">
                  <a-input v-model="queryParam.maxPower" allow-clear placeholder="请输入最大功率（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="最大扭矩（车型首页）">
                  <a-input v-model="queryParam.maxTorsion" allow-clear placeholder="请输入最大扭矩（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="能源类型（车型首页）">
                  <a-input v-model="queryParam.energyType" allow-clear placeholder="请输入能源类型（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="快充时间（车型首页）">
                  <a-input v-model="queryParam.fastChargingTime" allow-clear placeholder="请输入快充时间（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="慢充时间（车型首页）">
                  <a-input v-model="queryParam.slowChargingTime" allow-clear placeholder="请输入慢充时间（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="续航里程（车型首页）">
                  <a-input v-model="queryParam.enduranceMileage" allow-clear placeholder="请输入续航里程（车型首页）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="0,未删除 1,已删除">
                  <a-input v-model="queryParam.deleteFlag" allow-clear placeholder="请输入0,未删除 1,已删除"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="">
                  <a-input v-model="queryParam.createBy" allow-clear placeholder="请输入"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="">
                  <a-input v-model="queryParam.updateBy" allow-clear placeholder="请输入"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="">
                  <a-input v-model="queryParam.remark" allow-clear placeholder="请输入"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="">
                  <a-input v-model="queryParam.bmpId" allow-clear placeholder="请输入"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="数据状态（1：相似度较高，不用合并；2：匹配度较低，定义为新增；3：需要手动合并; 4:数据无效;6.在中台查出多条数据）">
                  <a-input v-model="queryParam.dataStatus" allow-clear placeholder="请输入数据状态（1：相似度较高，不用合并；2：匹配度较低，定义为新增；3：需要手动合并; 4:数据无效;6.在中台查出多条数据）"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="数据存储规则（1:车系不存在，新增数据至中台；2：年款相同，指导价相同，定义为同一车型，与中台车型建立关系">
                  <a-input v-model="queryParam.dataRule" allow-clear placeholder="请输入数据存储规则（1:车系不存在，新增数据至中台；2：年款相同，指导价相同，定义为同一车型，与中台车型建立关系"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="最高相似度">
                  <a-input v-model="queryParam.mostSimilar" allow-clear placeholder="请输入最高相似度"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('autohomeVehicleModel:add')" >
          <a-button type="primary" v-if="hasPerm('autohomeVehicleModel:add')" icon="plus" @click="$refs.addForm.add()">新增车型表</a-button>
          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('autohomeVehicleModel:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>
          <x-down
            v-if="hasPerm('autohomeVehicleModel:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('autohomeVehicleModel:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('autohomeVehicleModel:edit') & hasPerm('autohomeVehicleModel:delete')"/>
          <a-popconfirm v-if="hasPerm('autohomeVehicleModel:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import { autohomeVehicleModelPage, autohomeVehicleModelDelete, autohomeVehicleModelExport } from '@/api/modular/main/autohomevehiclemodel/autohomeVehicleModelManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      XDown
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '系统id',
            align: 'center',
            dataIndex: 'systemId'
          },
          {
            title: '汽车之家车型id',
            align: 'center',
            dataIndex: 'qczjModelId'
          },
          {
            title: '品牌id',
            align: 'center',
            dataIndex: 'brandId'
          },
          {
            title: '品牌名称',
            align: 'center',
            dataIndex: 'brandName'
          },
          {
            title: '车系id',
            align: 'center',
            dataIndex: 'seriesId'
          },
          {
            title: '车系名称',
            align: 'center',
            dataIndex: 'seriesName'
          },
          {
            title: '车型名称',
            align: 'center',
            dataIndex: 'modelName'
          },
          {
            title: '由于车辆型号是从车行168（大搜车旗下）爬取，所以用此ID与其对应。',
            align: 'center',
            dataIndex: 'dscModelId'
          },
          {
            title: '车型配置信息',
            align: 'center',
            dataIndex: 'modelJson'
          },
          {
            title: '车型编码',
            align: 'center',
            dataIndex: 'modelCode'
          },
          {
            title: '外观颜色',
            align: 'center',
            dataIndex: 'appearanceColor'
          },
          {
            title: '内饰颜色',
            align: 'center',
            dataIndex: 'interiorColor'
          },
          {
            title: '销售类型',
            align: 'center',
            dataIndex: 'saleType'
          },
          {
            title: '年款',
            align: 'center',
            dataIndex: 'modelYear'
          },
          {
            title: '排量(车型首页)',
            align: 'center',
            dataIndex: 'exhaust'
          },
          {
            title: '变速箱（车型首页）',
            align: 'center',
            dataIndex: 'shiftBox'
          },
          {
            title: '上市日期',
            align: 'center',
            dataIndex: 'launchDate'
          },
          {
            title: '车身结构',
            align: 'center',
            dataIndex: 'bodyTypeName'
          },
          {
            title: '发动机类型',
            align: 'center',
            dataIndex: 'engineTypeName'
          },
          {
            title: '级别（车型首页）',
            align: 'center',
            dataIndex: 'carGrade'
          },
          {
            title: '汽车类型',
            align: 'center',
            dataIndex: 'carType'
          },
          {
            title: '子品牌id',
            align: 'center',
            dataIndex: 'subBrandNo'
          },
          {
            title: '子品牌名称',
            align: 'center',
            dataIndex: 'subBrandName'
          },
          {
            title: '指导价',
            align: 'center',
            dataIndex: 'guidingPrice'
          },
          {
            title: '环保标准（车型首页）',
            align: 'center',
            dataIndex: 'emissionStandard'
          },
          {
            title: '综合油耗(工信部)（车型首页）',
            align: 'center',
            dataIndex: 'oilConsumption'
          },
          {
            title: '最大功率（车型首页）',
            align: 'center',
            dataIndex: 'maxPower'
          },
          {
            title: '最大扭矩（车型首页）',
            align: 'center',
            dataIndex: 'maxTorsion'
          },
          {
            title: '能源类型（车型首页）',
            align: 'center',
            dataIndex: 'energyType'
          },
          {
            title: '快充时间（车型首页）',
            align: 'center',
            dataIndex: 'fastChargingTime'
          },
          {
            title: '慢充时间（车型首页）',
            align: 'center',
            dataIndex: 'slowChargingTime'
          },
          {
            title: '续航里程（车型首页）',
            align: 'center',
            dataIndex: 'enduranceMileage'
          },
          {
            title: '0,未删除 1,已删除',
            align: 'center',
            dataIndex: 'deleteFlag'
          },
          {
            title: '',
            align: 'center',
            dataIndex: 'createBy'
          },
          {
            title: '',
            align: 'center',
            dataIndex: 'updateBy'
          },
          {
            title: '',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '',
            align: 'center',
            dataIndex: 'bmpId'
          },
          {
            title: '数据状态（1：相似度较高，不用合并；2：匹配度较低，定义为新增；3：需要手动合并; 4:数据无效;6.在中台查出多条数据）',
            align: 'center',
            dataIndex: 'dataStatus'
          },
          {
            title: '数据存储规则（1:车系不存在，新增数据至中台；2：年款相同，指导价相同，定义为同一车型，与中台车型建立关系',
            align: 'center',
            dataIndex: 'dataRule'
          },
          {
            title: '最高相似度',
            align: 'center',
            dataIndex: 'mostSimilar'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return autohomeVehicleModelPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
      }
    },
    created () {
      if (this.hasPerm('autohomeVehicleModel:edit') || this.hasPerm('autohomeVehicleModel:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.autohomeVehicleModelDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.autohomeVehicleModelDelete(paramIds)
      },
      autohomeVehicleModelDelete (record) {
        autohomeVehicleModelDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        autohomeVehicleModelExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
